package com.dongdongshop.serviec.brand;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.seller.TbSpecificationMapper;
import com.dongdongshop.mapper.seller.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.seller.TbSpecification;
import com.dongdongshop.pojo.seller.TbSpecificationOption;
import com.dongdongshop.pojo.seller.TbSpecificationOptionExample;
import com.dongdongshop.service.SpecificationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Resource
    private TbSpecificationMapper tbSpecificationMapper;
    @Resource
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;
    private final Logger logger = LoggerFactory.getLogger(SpecificationServiceImpl.class);

    @Override
    public List<TbSpecification> getSpecAll() {
        return tbSpecificationMapper.selectByExample(null);
    }

    @Override
    public int updateSpecification(TbSpecification tbSpecification, List<TbSpecificationOption> list) {
     try {
         tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
         //先删除在增加
         TbSpecificationOptionExample tso=new TbSpecificationOptionExample();
         TbSpecificationOptionExample.Criteria criteria = tso.createCriteria();
         criteria.andSpecIdEqualTo(tbSpecification.getId());
         tbSpecificationOptionMapper.deleteByExample(tso);
         tbSpecificationOptionMapper.insertBatch(tbSpecification.getId(),list);
         return 1;
     }catch (Exception e){
         logger.error("操作异常",e);
         return 0;
     }
    }

    @Override
    public TbSpecification getDataById(Long id) {
      TbSpecification tbSpecification=tbSpecificationMapper.selectByPrimaryKey(id);
      List<TbSpecificationOption> list=tbSpecificationOptionMapper.selectBySpec(id);
        tbSpecification.setList(list);
        return tbSpecification;
    }

    @Override
    public int add(TbSpecification specification, List<TbSpecificationOption> list) {
        try{
            tbSpecificationMapper.addSpecification(specification);
            System.out.println("id"+specification.getId());
            //添加specificationOption的数据
            tbSpecificationOptionMapper.insertBatch(specification.getId(),list);
            return 1;
        }catch(Exception e){
            logger.error("操作异常",e);
            return 0;
        }
    }

    @Override
    public int deleteByIds(Integer[] ids) {
        try {
            tbSpecificationMapper.deleteByIds(ids);
            //删除option
            tbSpecificationOptionMapper.deletBySpecId(ids);
            return 1;
        }catch (Exception e){
            logger.error("操作异常",e);
            return 0;
        }

    }

    @Override
    public List<TbSpecification> getInfo(String specName) {
        return tbSpecificationMapper.getInfo(specName);
    }
}
